<%--
  Created by IntelliJ IDEA.
  User: asus
  Date: 2019/12/16
  Time: 18:34
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
//首先判断用户是否记住登陆状态
    String []isUserCookie=request.getParameterValues("isUserCookie");
    String userName=request.getParameter("userName");
    String passWord=request.getParameter("passWord");
    Cookie userNameCookie=new Cookie("userName",userName);
    Cookie passWordCookie=new Cookie("passWord",passWord);
    if(isUserCookie!=null&&isUserCookie.length>0){   //判断是否勾选保存十天
        //把用户名和密码保存在cookie中
        response.addCookie(userNameCookie);
        response.addCookie(passWordCookie);
        userNameCookie.setMaxAge(864000);  //设置cookie的生命周期
        passWordCookie.setMaxAge(864000);   //设置cookie的生命周期
    }else{
        Cookie []cookie=request.getCookies();        //当不需要保存cookie时的操作
        if(cookie!=null&&cookie.length>0){
            for (Cookie c:cookie){
                if(c.getName().equals("userName")){
                    c.setMaxAge(0);
                    response.addCookie(c);
                }
                if(c.getName().equals("passWord")){
                    c.setMaxAge(0);
                    response.addCookie(c);
                }

            }
        }
    }
%>
<h1>欢迎登陆</h1>
<a href="cookieJsp.jsp" >跳转页面</a>
</body>
</html>
